/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.vranckaert.calendar.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import eu.vranckaert.calendar.R;

/**
 * An assortment of UI helpers.
 */
public class UIUtils {
    //private static StyleSpan sBoldSpan = new StyleSpan(Typeface.BOLD);

    private static final int BRIGHTNESS_THRESHOLD = 130;

    /**
     * Calculate whether a color is light or dark, based on a commonly known
     * brightness formula.
     *
     * @see {@literal http://en.wikipedia.org/wiki/HSV_color_space%23Lightness}
     */
    public static boolean isColorDark(int color) {
        return ((30 * Color.red(color) +
                59 * Color.green(color) +
                11 * Color.blue(color)) / 100) <= BRIGHTNESS_THRESHOLD;
    }

    public static long getCurrentTime() {
        return System.currentTimeMillis();
    }

    /**
     * Create a background that is clickable using the standard colors and a configurable (non focused) background
     * color.
     * @param context The context.
     * @param backgroundColor The color of the background.
     * @return The background drawable.
     */
    public static Drawable createBackground(Context context, int backgroundColor) {
        ColorDrawable background = new ColorDrawable(backgroundColor);
        ColorDrawable backgroundFocus = new ColorDrawable(context.getResources().getColor(R.color.calendar_focus));

        int stateFocused = android.R.attr.state_focused;
        int statePressed = android.R.attr.state_pressed;

        StateListDrawable sld = new StateListDrawable();
        sld.addState(new int[]{ stateFocused,  statePressed}, backgroundFocus);
        sld.addState(new int[]{-stateFocused,  statePressed}, backgroundFocus);
        sld.addState(new int[]{-stateFocused}, background);
        return sld;
    }
}
